//
//  WKLoginVC.m
//  WuKongLogin
//
//  Created by tt on 2019/12/1.
//

#import "WKLoginVC.h"
#import "WKLoginView.h"
#import <WuKongUsernameLogin/WuKongUsernameLogin-Swift.h>
#import "WKMnemonicsImportVC.h"
#import "WKUsernameLoginVM.h"

@interface WKLoginVC ()
 
@property(nonatomic,strong) WKLoginView  *loginView;

@end

@implementation WKLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.hidden= YES;
    [self fillZoneAndPhone];
}

- (NSString *)langTitle {
    return LLang(@"登录");
}

- (WKBaseVM *)viewModel {
    return [WKLoginVM new];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
   
}

-(void) loadView {
    //[super loadView];

    self.loginView = [[WKLoginView alloc] initWithFrame:CGRectMake(0, 0, WKScreenWidth, WKScreenHeight)];
   
     __weak typeof(self) weakSelf = self;
    
    self.loginView.onLogin = ^(NSString * _Nonnull username, NSString * _Nonnull password,NSString *country) {
        UIView *topView = WKNavigationManager.shared.topViewController.view;
        if(!username || username.length == 0) {
            [topView showHUDWithHide:LLangW(@"账号不能为空！",weakSelf)];
            return;
        }
        if(!password || password.length == 0) {
            [topView showHUDWithHide:LLangW(@"密码不能为空！",weakSelf)];
            return;
        }
        [topView showHUD];
        [WKAPIClient.sharedClient POST:@"user/login" parameters:@{
            @"username":username,
            @"password": password,
        }].then(^(NSDictionary *result){
            [topView hideHud];
            WKLoginResp *resp = (WKLoginResp*)[WKLoginResp fromMap:result type:ModelMapTypeAPI];
            if([WKWeb3Helper existUserWallet:resp.uid]) {
                [WKUsernameLoginVM handleLoginData:resp isSave:YES];
                 WKApp.shared.loginInfo.extra[@"finishedWeb3Veritfy"] = @"1";
                [WKApp.shared.loginInfo save];
                [[WKApp shared] invoke:WKPOINT_LOGIN_SUCCESS param:nil];
            } else {
                [WKUsernameLoginVM handleLoginData:resp isSave:NO];
                WKMnemonicsImportVC *vc = [WKMnemonicsImportVC new];
                [WKNavigationManager.shared pushViewController:vc animated:YES];
            }
        }).catch(^(NSError *error){
            [topView switchHUDError:error.domain];
        });
    };
    self.view = self.loginView;
}

-(void) fillZoneAndPhone {
    NSString *currentMobile = [WKApp shared].loginInfo.extra[@"phone"];
       NSString *currentCountry = [WKApp shared].loginInfo.extra[@"zone"];
       if(currentMobile && ![currentMobile isEqualToString:@""]) {
           self.loginView.mobile = currentMobile;
       }
       if(currentCountry && ![currentCountry isEqualToString:@""]) {
           self.loginView.country = [currentCountry stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:@""];
       }
}

- (void)viewConfigChange:(WKViewConfigChangeType)type {
    [self.loginView viewConfigChange:type];
}

@end
